﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

using YT.DB;
using YT.Modules;
using YT.Commons;  

namespace YT.XH.Others
{
    public partial class frmOthersSickHistory : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrSickRecID = string.Empty;
        private string mstrCurPnl = string.Empty;
        ArrayList marrlist = new ArrayList();
        ArrayList marrPnl = new ArrayList();
        ArrayList marrAllPnls = new ArrayList();
        ArrayList marrMsg = new ArrayList();
        private bool mblnExit = false;
        public frmOthersSickHistory()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmOthersSickHistory(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        private void dateTimeEx2_Load(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                UpdateData();
            }
            this.Close();   
        }

        private void frmOthersSickHistory_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
            FillData();            
            this.Capture = true;
            this.Text = "病史调查";            
            timer1.Start();
            Cursor = Cursors.Default; 
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel")
                {
                    marrAllPnls.Add(con);
                }
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "RadioButton") ((RadioButton)con).CheckedChanged += new EventHandler(this.CalScore);
            }
        }

        private void FillData()
        {

            string strTableName = "titleResult";

            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }

            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select * from " + strTableName + " where   SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS + "'";
            DataView dvFill = dbcon.GetDataTable(strSQL).DefaultView;
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;                    
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            //string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS + "'";
                            dvFill.RowFilter = "rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS + "'";
                            if (dvFill.Count  == 0 ) continue;
                            strResult = CvtFunc.ToString(dvFill[0]["Result"]);
                            if (conText.GetType().Name == "TextBox")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                            }
                            else if (conText.GetType().Name == "DateTimeEx")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            dbcon.CloseCon();
            CalScoreAndSub();
            CalTHI();
        }
        private void CalScore(object sender, EventArgs e)
        {
            CalScoreAndSub();
            CalTHI();
        }
        private bool SpecialCheck()
        {
            if (radHasEM.Checked)
            {
                if (txtTotalScore.Text == "" || txtTotalScore.Text == "0")
                {
                    MessageBox.Show("您必须填写  耳鸣致残量表  。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (radHasXY.Checked)
            {
                if (txtTHITotal.Text == "" || txtTHITotal.Text == "0")
                {
                    MessageBox.Show("您必须填写  眩晕障碍评估表  。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            return true;
        }

        private bool UpdateData()
        {
            if (!SpecialCheck()) return false;
            try
            {
                CalScoreAndSub();
                CalTHI();
                string strTableName = "titleResult";
                DBAccess dbcon = new DBAccess(false, false);
                foreach (Control conDtl in marrlist)
                {
                    if (conDtl.GetType().Name == "Panel")
                    {
                        string[] strInfoSeqNo = new string[2];
                        strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                        string strHdrSeqNo = strInfoSeqNo[0];
                        string strDtlSeqNo = strInfoSeqNo[1];
                        string strSickRecID = mstrSickRecID;
                        string strColTitle = string.Empty;
                        string strResult = string.Empty;
                        DataRow drRow = null;
                        string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type";
                        foreach (Control conText in conDtl.Controls)
                        {
                            if (conText.Tag != "" && conText.Tag != null)
                            {
                                if (conText.GetType().Name == "TextBox")
                                {
                                    strResult = conText.Text;
                                }
                                else if (conText.GetType().Name == "DateTimePicker")
                                {
                                    strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString(), DateFormatOption.DateFmtYMD);
                                }
                                else if (conText.GetType().Name == "DateTimeEx")
                                {
                                    if (conText.Text == "    -  -  ")
                                        strResult = "";
                                    else
                                        strResult = conText.Text;
                                }
                                else if (conText.GetType().Name == "RadioButton")
                                {
                                    strResult = (((RadioButton)conText).Checked ? "true" : "false");
                                }
                                else if (conText.GetType().Name == "CheckBox")
                                {
                                    strResult = (((CheckBox)conText).Checked ? "true" : "false");
                                }
                                else if (conText.GetType().Name == "Label")
                                {
                                    strResult = "";
                                    continue;
                                }
                                strColTitle = conText.Tag.ToString();

                                string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID ='" + mstrSickRecID + "' and Type='" + TitleType.OTHERS + "'";
                                bool blnIsNew = false;
                                DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                                if (drCheck == null)
                                {
                                    blnIsNew = true;
                                    drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                                }
                                else
                                {
                                    blnIsNew = false;
                                    drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                                }

                                drRow["SickRecID"] = strSickRecID;
                                drRow["HdrSeqno"] = strHdrSeqNo;
                                drRow["DtlSeqNo"] = strDtlSeqNo;
                                drRow["RowNo"] = strColTitle;
                                drRow["Result"] = strResult;
                                drRow["Type"] = TitleType.OTHERS;

                                if (blnIsNew)
                                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                                else
                                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                            }
                        }
                    }
                }
                dbcon.CloseCon();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateData())
                this.Close();
        }

        private void CalScoreAndSub()
        {
            int intResult = 0;
            int intFscore = 0;
            int intEscore = 0;
            int intPscore = 0;
            int intTotalScore = 0;
            int intTotalSubScore = 0;
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    if (conDtl.Name.Substring(0, 4) == "pnl_")
                    {
                        string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intResult = BaseFunctions.GetScoreFunction(conItem.Tag.ToString());
                                }
                                if (strType == "E") intEscore = intEscore + intResult;
                                if (strType == "F") intFscore = intFscore + intResult;
                                if (strType == "P") intPscore = intPscore + intResult;
                                intResult = 0;
                            }
                        }
                    }
                    else if (conDtl.Name.Length >= 8 && conDtl.Name.Substring(0, 7) == "pnlsub_")
                    {
                        int intSubResult = 0;
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intSubResult = BaseFunctions.GetScoreFunction(conItem.Tag.ToString());
                                }
                            }
                            intTotalSubScore = intTotalSubScore + intSubResult;
                            intSubResult = 0;
                        }
                    }
                }
            }

            intTotalScore = intFscore + intEscore + intPscore;
            txtEscore.Text = CvtFunc.ToString(intEscore);
            txtPscore.Text = CvtFunc.ToString(intPscore);
            txtFscore.Text = CvtFunc.ToString(intFscore);

            txtTotalScore.Text = CvtFunc.ToString(intTotalScore);
            txtSubTotalScore.Text = CvtFunc.ToString(intTotalSubScore);

        }

        private void CalTHI()
        {
            int intResult = 0;
            int intFscore = 0;
            int intEscore = 0;
            int intCscore = 0;
            int intTotalScore = 0;
            int intTotalSubScore = 0;
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    if (conDtl.Name.Substring(0, 5) == "pnl2_")
                    {
                        string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intResult = BaseFunctions.GetTHIScoreFunction(conItem.Tag.ToString());
                                }
                                if (strType == "E") intEscore = intEscore + intResult;
                                if (strType == "F") intFscore = intFscore + intResult;
                                if (strType == "C") intCscore = intCscore + intResult;
                                intResult = 0;
                            }
                        }
                    }
                }
            }

            intTotalScore = intFscore + intEscore + intCscore;
            txtTHIEscroe.Text = CvtFunc.ToString(intEscore);
            txtTHICscore.Text = CvtFunc.ToString(intCscore);
            txtTHIFscore.Text = CvtFunc.ToString(intFscore);

            txtTHITotal.Text = CvtFunc.ToString(intTotalScore);

        }


        private void datDate1_Leave(object sender, EventArgs e)
        {
            if (datDate1.Text != "    -  -  ") datDate2.Text = "";
        }

        private void datDate2_Leave(object sender, EventArgs e)
        {
            if (datDate2.Text != "    -  -  ") datDate1.Text = "";
        }

        private void datDate3_Leave(object sender, EventArgs e)
        {
            if (datDate3.Text != "    -  -  ") datDate4.Text = "";
        }

        private void datDate4_Leave(object sender, EventArgs e)
        {
            if (datDate4.Text != "    -  -  ") datDate3.Text = "";
        }

        private void frmSickHistory_FormClosed(object sender, FormClosedEventArgs e)
        {            
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            CalScoreAndSub();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void ChangeOpacity()
        {
            if (mblnExit == false)
            {
                if (this.Opacity >= 1)
                    timer1.Stop();
                this.Opacity += 0.04;
            }
            else if (mblnExit == true)
            {
                if (this.Opacity <= 0.1)
                {
                    timer1.Stop();
                }
                this.Opacity -= 0.04;
            }
        }


        private void ValueCheck(object sender, EventArgs e)
        {
            string strConName = ((Control)sender).Name;
            Control con = ((Control)sender);
            #region 眩晕持续时间
            if (strConName == this.txtLastMin.Name)
            {
                if (con.Text.Trim() != "")
                {
                    txtLastOther.Text = "";
                    txtLastHour.Text = "";
                }
            }
            else if (strConName == this.txtLastHour.Name)
            {
                if (con.Text.Trim() != "")
                {
                    txtLastOther.Text = "";
                    txtLastMin.Text = "";
                }
            }
            else if (strConName == this.txtLastOther.Name)
            {
                if (con.Text.Trim() != "")
                {
                    txtLastMin.Text = "";
                    txtLastHour.Text = "";
                }
            }
            #endregion

            #region  是否伴有跌倒
            if (strConName == textBox53.Name)
            {
                if (textBox53.Text.Trim() != "")
                    radioButton171.Checked = true;
                else
                    radioButton172.Checked = false;
            }

            if (strConName == radioButton171.Name) //是
            {

            }
            else if (strConName == radioButton172.Name) //否
            {
                textBox53.Text = "";
            }
            #endregion

            #region  眩晕家族史
            if (strConName == textBox9.Name)
            {
                if (textBox9.Text.Trim() != "")
                    radioButton92.Checked = true;
                else
                    radioButton92.Checked = false;
            }

            if (strConName == radioButton92.Name) //是
            {

            }
            else if (strConName == radioButton91.Name) //否
            {
                textBox9.Text = "";
            }
            #endregion
            #region  梅尼埃病家族史
            if (strConName == textBox8.Name)
            {
                if (textBox8.Text.Trim() != "")
                    radioButton68.Checked = true;
                else
                    radioButton68.Checked = false;
            }

            if (strConName == radioButton68.Name) //是
            {

            }
            else if (strConName == radioButton67.Name) //否
            {
                textBox8.Text = "";
            }
            #endregion

            #region  其他遗传性疾病史
            if (strConName == textBox21.Name)
            {
                if (textBox21.Text.Trim() != "")
                    radioButton74.Checked = true;
                else
                    radioButton74.Checked = false;
            }

            if (strConName == radioButton74.Name) //是
            {

            }
            else if (strConName == radioButton71.Name) //否
            {
                textBox21.Text = "";
            }
            #endregion

            #region 相关既往史
            #region  听力问题
            if (strConName == textBox23.Name)
            {
                if (textBox23.Text.Trim() != "")
                    radioButton88.Checked = true;
                else
                    radioButton88.Checked = false;
            }

            if (strConName == radioButton87.Name) //是
            {

            }
            else if (strConName == radioButton87.Name) //否
            {
                textBox23.Text = "";
            }
            #endregion

            #region  视力问题
            //if (strConName == textBox24.Name)
            //{
            //    if (textBox24.Text.Trim() != "")
            //        radioButton90.Checked = true;
            //    else
            //        radioButton90.Checked = false;
            //}

            //if (strConName == radioButton90.Name) //是
            //{

            //}
            //else if (strConName == radioButton89.Name) //否
            //{
            //    textBox24.Text = "";
            //}
            #endregion

            #region  食物过敏
            if (strConName == textBox25.Name)
            {
                if (textBox25.Text.Trim() != "")
                    radioButton94.Checked = true;
                else
                    radioButton94.Checked = false;
            }

            if (strConName == radioButton94.Name) //是
            {

            }
            else if (strConName == radioButton93.Name) //否
            {
                textBox25.Text = "";
            }
            #endregion
            #region  吸入过敏
            if (strConName == textBox26.Name)
            {
                if (textBox26.Text.Trim() != "")
                    radioButton96.Checked = true;
                else
                    radioButton96.Checked = false;
            }

            if (strConName == radioButton96.Name) //是
            {

            }
            else if (strConName == radioButton95.Name) //否
            {
                textBox26.Text = "";
            }
            #endregion

            #region 药物过敏
            if (strConName == textBox27.Name)
            {
                if (textBox27.Text.Trim() != "")
                    radioButton98.Checked = true;
                else
                    radioButton98.Checked = false;
            }

            if (strConName == radioButton98.Name) //是
            {

            }
            else if (strConName == radioButton97.Name) //否
            {
                textBox27.Text = "";
            }
            #endregion

            if (strConName == textBox28.Name)
            {
                if (textBox28.Text.Trim() != "")
                    radioButton112.Checked = true;
                else
                    radioButton112.Checked = false;
            }

            if (strConName == radioButton112.Name) //是
            {

            }
            else if (strConName == radioButton111.Name) //否
            {
                textBox28.Text = "";
            }


            #endregion

            if (strConName == textBox29.Name)
            {
                if (textBox29.Text.Trim() != "")
                    radioButton114.Checked = true;
                else
                    radioButton114.Checked = false;
            }

            if (strConName == radioButton114.Name) //是
            {

            }
            else if (strConName == radioButton113.Name) //否
            {
                textBox29.Text = "";
            }
                                   
            #region 6.外伤手术史
            
            if (strConName == textBox31.Name)
            {
                if (textBox31.Text.Trim() != "")
                    radioButton122.Checked = true;
                else
                    radioButton122.Checked = false;
            }

            if (strConName == radioButton122.Name) //是
            {

            }
            else if (strConName == radioButton121.Name) //否
            {
                textBox31.Text = "";
            }
            #endregion
        }

    }
}
